<template>
  <j-modal
    :title="title"
    :width="width"
    :visible="visible"
    :fullscreen="fullscreen"
    switchFullscreen
    @ok="handleOk"
    :okButtonProps="{ class:{'jee-hidden': disableSubmit} }"
    @cancel="handleCancel"
    okText="Save"
    cancelText="Close">

    <template slot="footer">

      <a-popconfirm title="Confirm to Delete?" v-if="delVisible"  ok-text="Yes" cancel-text="No" @confirm="() => handleDelete()">
        <a-button key="Delete" type="primary">
          Delete
        </a-button>
      </a-popconfirm>
      <a-button key="Save" type="primary"  @click="handleOk">
        Save
      </a-button>
      <a-button key="Close"  @click="handleCancel">
        Close
      </a-button>
    </template>

    <course-info-form ref="realForm" @ok="submitCallback" :disabled="disableSubmit"></course-info-form>
  </j-modal>
</template>

<script>

  import CourseInfoForm from './CourseInfoForm'
  import {deleteAction} from "@api/manage";
  export default {
    name: 'CourseInfoModal',
    components: {
      CourseInfoForm
    },
    data () {
      return {
        id:-1,
        fullscreen:true,
        title:'',
        width:1300,
        delVisible:false,
        visible: false,
        disableSubmit: false,
        url: {
          delete: "/sistersophie/courseInfo/delete"
        },
      }
    },
    methods: {
      handleDelete: function () {
        console.info('id -->' + this.id);
        if(this.id < 0){
          return;
        }
        if(!this.url.delete){
          this.$message.error("请设置url.delete属性!")
          return
        }
        var that = this;
        deleteAction(that.url.delete, {id: this.id}).then((res) => {
          if (res.success) {
            //重新计算分页问题
            this.submitCallback();
          } else {
            that.$message.warning(res.message);
          }
        });
      },
      add () {
        this.id = -1;
        this.visible=true;
        this.delVisible = false;
        this.$nextTick(()=>{
          this.$refs.realForm.add();
        })
      },
      edit (record) {
        this.visible=true
        this.id = record.id;
        this.delVisible = true;
        this.$nextTick(()=>{
          this.$refs.realForm.edit(record);
        })
      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        this.$refs.realForm.submitForm();
      },
      submitCallback(){
        this.$emit('ok');
        this.visible = false;
      },
      handleCancel () {
        this.close()
      }
    }
  }
</script>